package com.mibox.common.utils;

import com.blankj.utilcode.util.FileIOUtils;

public class LEDUtils {

    public static final String BLUE_LED = "sys/class/leds/blue_led/brightness";
    public static final String GREEN_LED = "sys/class/leds/green_led/brightness";
    public static final String RED_LED = "sys/class/leds/red_led/brightness";

    public static String[] LED_FILES = {RED_LED, GREEN_LED, BLUE_LED};
    public static String[] ledData = {"0", "0", "0"};
    public static int LED_IDX_RED = 0;
    public static int LED_IDX_GREEN = 1;
    public static int LED_IDX_BLUE = 2;

    public static void turnOn(int index) {
        if (index < LED_FILES.length) {
            try {
                FileIOUtils.writeFileFromString(LED_FILES[index], "1");
                ledData[index] = FileIOUtils.readFile2String(LED_FILES[index]).trim();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    public static void readLEDData() {
        for (int i = 0; i < LED_FILES.length; i++) {
            ledData[i] = FileIOUtils.readFile2String(RED_LED).trim();
        }
    }

    public static void toggleLEDs() {
        readLEDData();
        for (int i = 0; i < LED_FILES.length; i++) {
            FileIOUtils.writeFileFromString(LED_FILES[i], ledData[i].equals("0") ? "1" : "0");
        }
    }

    public static void setAllLEDs(boolean light) {
        String str = light ? "1" : "0";
        for (String ledFile : LED_FILES) {
            FileIOUtils.writeFileFromString(ledFile, str);
        }
        readLEDData();
    }
}
